//权限管理

/**
 * 添加/编辑视图模型
 */
interface IAuthorizationManagentDetailViewModel {
    /**
     * 编号
     */
    id?: string;
    /**
     * 权限Tag
     */
    tag: string;
    /**
     * 控制器
     */
    controller: string;
    /**
     * 动作
     */
    action: string;
    /**
     * 所属权限组
     */
    authorizationTypeId: string;
    /**
     * 是否启用
     */
    isEnabled: boolean;
    /**
     * 说明
     */
    description: string;
    /**
     * 保存事件
     */
    click: () => void;
}

/**
 * 视图模型
 */
interface IAuthorizationManagentViewModel extends IBaseViewModel<IAuthorizationManagentQueryTerms> {
    /**
     * 填充权限分类下拉
     */
    authorizationTypeDropDown: Array<AuthorizationTypeListItemResponse>;
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<AuthorizationListItemResponse>;
    /**
     * 添加/编辑视图
     */
    detail: IAuthorizationManagentDetailViewModel;
    /**
     * 添加权限
     */
    addAuthorization: () => void;
    /**
     * 编辑权限
     */
    editAuthorization: (id: string) => void;
    /**
     * 删除权限
     */
    delAuthorization: (id: string) => void;
    /**
     * 启用权限
     */
    enableAuthorization: (id: string) => void;
    /**
     * 禁用权限
     */
    disableAuthorization: (id: string) => void;
    /**
     * 查询
     */
    query: () => void;
    /**
     * 列表高度
     */
    heightA:number
}

/**
 * 查询条件
 */
interface IAuthorizationManagentQueryTerms {
    /**
     * 权限类别编号
     */
    authorizationTypeId: string;
    /**
     * 权限Tag
     */
    authorizationTag: string;
}

class AuthorizationManagentController extends ListController<IAuthorizationManagentViewModel, IAuthorizationManagentQueryTerms> {
    gridApi: uiGrid.IGridApiOf<AuthorizationListItemResponse>;
    
    constructor($scope: IAuthorizationManagentViewModel, private authorizationService: IAuthorizationService,
        private authorizationTypeService: IAuthorizationTypeService, private $ngBootbox: BootboxService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        this.$scope.addAuthorization = this.showAddAuthorizationDialog;
        this.$scope.editAuthorization = this.showEditAuthorizationDialog;
        this.$scope.enableAuthorization = this.showEnableAuthorizationDialog;
        this.$scope.disableAuthorization = this.showDisableAuthorizationDialog;
        this.$scope.delAuthorization = this.showDeleteAuthorizationDialog;

        this.$scope.queryTerms = {
            authorizationTag: "",
            authorizationTypeId: ""
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false },
                { name: "权限Tag", field: "tag", enableColumnMenu: false },
                { name: "权限类别", field: "category", enableColumnMenu: false },
                { name: "权限级别", field: "level", enableColumnMenu: false },
                { name: "控制器", field: "controller", enableColumnMenu: false },
                { name: "动作", field: "action", enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: "checkmark",
                    cellTemplate: '<a data-toggle="modal" data-target="#myModal" class="fa fa-edit m-l-xs primary text-info" title="编辑" ng-click="grid.appScope.editAuthorization(row.entity.id)"></a> <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.delAuthorization(row.entity.id)"></a> <a class="fa fa-check-circle primary text-success" ng-click="grid.appScope.enableAuthorization(row.entity.id)" title="启用" ng-hide="row.entity.isEnabled"></a> <a class="fa fa-minus-circle primary text-primary" ng-show="row.entity.isEnabled" title="禁用" ng-click="grid.appScope.disableAuthorization(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.$scope.query = this.query;
        this.query();
        this.loadAuthorizationTypeDropDown();
    }

    /**
     * 填充权限分类下拉组件
     */
    loadAuthorizationTypeDropDown(): void {
        this.authorizationTypeService.getList("", 0, -1).then(result => {
            this.$scope.authorizationTypeDropDown = result.data.data;
        });
    }

    loadData(queryTerms: IAuthorizationManagentQueryTerms, skip: number, count: number): void {
        this.authorizationService.getList(queryTerms.authorizationTag, queryTerms.authorizationTypeId, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            result.data.data.map((item, index) => item.index = skip++ +1);
            this.$scope.gridOptions.data = result.data.data;

            
            this.$scope.heightA = (result.data.data.length* 39)+95;
            

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    /**
     * 显示添加权限窗口
     */
    showAddAuthorizationDialog = () => {
        this.$scope.title = "添加权限";
        this.$scope.detail = {
            tag: "",
            controller: "",
            action: "",
            authorizationTypeId: "",
            isEnabled: true,
            description: "",
            click: this.addAuthorization
        };
    }

    addAuthorization = ()=>{
        if (!$("#AuthForm").valid()) { return };
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.authorizationService.add(
                this.$scope.detail.tag,
                this.$scope.detail.controller,
                this.$scope.detail.action,
                this.$scope.detail.authorizationTypeId,
                this.$scope.detail.isEnabled,
                this.$scope.detail.description
            ).then(result => {
                this.reload();
                $('#myModal').modal('hide');
            })
        });
    }

    /**
     * 显示编辑权限窗口
     */
    showEditAuthorizationDialog = (id: string) => {
        var validator = $('#AuthForm').validate();
        validator.form();
        validator.hideErrors();
        $(".error").removeClass("error");
        this.authorizationService.getDetail(id).then(result => {
            this.$scope.detail = {
                id: id,
                tag: result.data.tag,
                controller: result.data.controller,
                action: result.data.action,
                authorizationTypeId: result.data.categoryId,
                isEnabled: result.data.isEnabled,
                description: result.data.description,
                click: this.editAuthorization
            };
        });
    }

    editAuthorization = ()=> {
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.authorizationService.edit(
                this.$scope.detail.id,
                this.$scope.detail.tag,
                this.$scope.detail.controller,
                this.$scope.detail.action,
                this.$scope.detail.authorizationTypeId,
                this.$scope.detail.isEnabled,
                this.$scope.detail.description
            ).then(result => {
                this.reload();
                $('#myModal').modal('hide');
            });
        });
    }

    showDeleteAuthorizationDialog = (id: string) => {
        this.$ngBootbox.confirm("是否删除该权限？").then(e => {
            this.authorizationService.delete(id).then(result => {
                this.reload();
            });
        });
    }

    showEnableAuthorizationDialog = (id: string) => {
        this.$ngBootbox.confirm("是否启用该权限？").then(e => {
            this.authorizationService.disabled(id, true).then(result => {
                this.reload();
            });
        });
    }

    showDisableAuthorizationDialog = (id: string) => {
        this.$ngBootbox.confirm("是否禁用该权限？").then(e => {
            this.authorizationService.disabled(id, false).then(result => {
                this.reload();
            });
        });
    }
}

angular.module("tms").controller('authorizationManagentController', ['$scope', 'authorizationService', 'authorizationTypeService', '$ngBootbox', AuthorizationManagentController]);